package web.suzy.oj.controller.oj;

import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import web.suzy.oj.annotation.AnonApi;
import web.suzy.oj.common.result.ResultBean;
import web.suzy.oj.pojo.dto.ApplyResetPasswordDTO;
import web.suzy.oj.pojo.dto.LoginDTO;
import web.suzy.oj.pojo.dto.RegisterDTO;
import web.suzy.oj.pojo.dto.ResetPasswordDTO;
import web.suzy.oj.pojo.vo.RegisterCodeVO;
import web.suzy.oj.pojo.vo.UserInfoVO;
import web.suzy.oj.service.oj.PassportService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * YangSuzy 软件工作室
 * 类名: PassportController
 * 描述: 用户通行证控制器
 * 功能: 处理登录、注册、重置密码
 *
 * @author YangSuzy
 * Date: 2022/11/9 22:45
 */
@RestController
@RequestMapping("/api")
public class PassportController {
    @Autowired
    private PassportService passportService;

    /**
     * 方法名: login
     * 描述: 用户登录方法
     *
     * @param loginDto 登录数据实体类
     * @param response 响应
     * @param request  请求
     * @return web.suzy.oj.common.result.ResultBean<web.suzy.oj.pojo.vo.UserInfoVO>
     * @date 2022/11/9 22:48
     * @auther YangSuzy
     **/
    @PostMapping("/login")
    @AnonApi
    public ResultBean<UserInfoVO> login(@Validated @RequestBody LoginDTO loginDto, HttpServletResponse response, HttpServletRequest request) {
        return passportService.login(loginDto, response, request);
    }

    /**
     * 方法名: getRegisterCode
     * 描述: 向注册用户发送验证码
     *
     * @param email 注册用户邮箱
     * @return web.suzy.oj.common.result.ResultBean<web.suzy.oj.pojo.vo.RegisterCodeVO>
     * @date 2022/11/14 12:04
     * @auther YangSuzy
     **/
    @RequestMapping(value = "/get-register-code", method = RequestMethod.GET)
    @AnonApi
    public ResultBean<RegisterCodeVO> getRegisterCode(@RequestParam(value = "email") String email) {
        return passportService.getRegisterCode(email);
    }

    /**
     * 方法名: applyResetPassword
     * 描述: 通过邮件发送重置密码链接
     *
     * @param applyResetPasswordDto
     * @return web.suzy.oj.common.result.ResultBean<java.lang.Void>
     * @date 2022/11/14 13:07
     * @auther YangSuzy
     **/
    @PostMapping("/apply-reset-password")
    @AnonApi
    public ResultBean<Void> applyResetPassword(@RequestBody ApplyResetPasswordDTO applyResetPasswordDto) {
        return passportService.applyResetPassword(applyResetPasswordDto);
    }

    /**
     * 方法名: register
     * 描述: 用户注册方法
     *
     * @param registerDto 注册用户数据传输对象
     * @return web.suzy.oj.common.result.ResultBean<java.lang.Void>
     * @date 2022/11/9 22:51
     * @auther YangSuzy
     **/
    @PostMapping("/register")
    @AnonApi
    public ResultBean<Void> register(@Validated @RequestBody RegisterDTO registerDto) {
        return passportService.register(registerDto);
    }

    /**
     * 方法名: resetPassword
     * 描述: 密码重置方法
     *
     * @param resetPasswordDto 密码重置数据传输对象
     * @return web.suzy.oj.common.result.ResultBean<java.lang.Void>
     * @date 2022/11/9 22:52
     * @auther YangSuzy
     **/
    @PostMapping("/reset-password")
    @AnonApi
    public ResultBean<Void> resetPassword(@RequestBody ResetPasswordDTO resetPasswordDto) {
        return passportService.resetPassword(resetPasswordDto);
    }

    /**
     * 方法名: logout
     * 描述: 用户登出，将登陆凭证从redis中删除
     *
     * @return web.suzy.oj.common.result.ResultBean<java.lang.Void>
     * @date 2022/11/15 12:51
     * @auther YangSuzy
     **/
    @GetMapping("/logout")
    @RequiresAuthentication
    public ResultBean<Void> logout() {
        return passportService.logout();
    }
}
